package com.planner.core.util.exception;

import org.apache.commons.logging.Log;

import com.planner.core.constants.LogConstants;

/**
 * <p>
 * The abstract base exception for all checked exceptions within the casm
 * application.
 * </p>
 * @author Simon Evans
 */
public abstract class BaseException extends Exception {
    /**
	 * 
	 */
	private static final long serialVersionUID = -614316231342368379L;

	/**
     * <p>
     * This constructor will log this exception to the specified log at the
     * specified log severity.
     * </p>
     * @param log The log that this exception should be logged to.
     * @param severity The severity at which this exception should be logged.
     * @param message Message describing the circumstances of the exception, and
     *            all relevant context information.
     */
    public BaseException(Log log, int severity, String message) {
        super(message);
        logMessage(log, severity, message);
    }

    /**
     * <p>
     * This constructor will log this exception to the specified log at the
     * specified log severity.
     * </p>
     * @param log The log that this exception should be logged to.
     * @param severity The severity at which this exception should be logged.
     * @param message Message describing the circumstances of the exception, and
     *            all relevant context information.
     * @param cause The exception that is the cause of this exception
     */
    public BaseException(Log log, int severity, String message, Throwable cause) {
        super(message, cause);
        logMessage(log, severity, message);
    }

    /**
     * <p>
     * Constructor, to be used when there is no root cause exception.
     * </p>
     * @param message Message describing the circumstances of the exception, and
     *            all relevant context information.
     */
    public BaseException(String message) {
        super(message);
    }

    /**
     * <p>
     * Constructor.
     * </p>
     * @param message Message describing the circumstances of the exception, and
     *            all relevant context information.
     * @param cause The exception that is the cause of this exception
     */
    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    /**
     * @param log
     * @param severity
     * @param message
     */
    private void logMessage(Log log, int severity, String message) {
        switch (severity) {
        case LogConstants.SEVERITY_DEBUG:
            log.debug(message, this);
            break;
        case LogConstants.SEVERITY_ERROR:
            log.error(message, this);
            break;
        case LogConstants.SEVERITY_FATAL:
            log.fatal(message, this);
            break;
        case LogConstants.SEVERITY_INFO:
            log.info(message, this);
            break;
        case LogConstants.SEVERITY_TRACE:
            log.trace(message, this);
            break;
        case LogConstants.SEVERITY_WARN:
            log.warn(message, this);
            break;
        default:
        }
    }
}

